//
//  YJDetaViewController.m
//  健康饮食
//
//  Created by qianfeng01 on 15/7/4.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "YJDetaViewController.h"
#import "YJDetaCell.h"
#import "YJDetaModel.h"
#import "AFNetworking.h"
#import "YJDetadetaViewController.h"
@interface YJDetaViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tableView;
    NSMutableArray *_dataArray;
    AFHTTPRequestOperationManager *_manager;
    
}
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) AFHTTPRequestOperationManager *manager;

@end

@implementation YJDetaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;

    self.dataArray=[[NSMutableArray alloc] init];
    NSString *cellName = @"YJDetaCell";
    [self creatTableView:cellName];
    self.tableView.rowHeight=200;
    
    [self creatHttpRequest];
    [self loadData];
}

-(void)loadData{
    __weak typeof (self)weakSelf =self;
    NSString *url =[NSString stringWithFormat:kUrlDeta,(long)self.code];
    [weakSelf.manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            NSDictionary * detaDict =[NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
            NSDictionary *dicts =detaDict[@"data"];
            NSArray *array = dicts[@"dishs"];
            for (NSDictionary *dict in array) {
                YJDetaModel *model =[[YJDetaModel alloc] init];
                model.dishsName =dict[@"name"];
                model.allClick =dict[@"allClick"];
 
                model.image =dict[@"img"];
                model.code = dict[@"code"];
                [self.dataArray addObject:model];
            }
            [self.tableView reloadData];
            
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YJDetaCell *cell =[tableView dequeueReusableCellWithIdentifier:@"YJDetaCell" forIndexPath:indexPath];
    YJDetaModel *model =self.dataArray[indexPath.row];
    [cell showDataWithModel:model];
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return self.name;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    YJDetadetaViewController *datecontroller =[[YJDetadetaViewController alloc] init];
    YJDetaModel *model =self.dataArray[indexPath.row];
    datecontroller.code =model.code;
    [self.navigationController pushViewController:datecontroller animated:YES];
}
@end
